<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace Think;

/**
 * 加密解密类
 */
class Crypt {

    private static $handler = '';

    public static function init($type = '') {
        $type = $type? : C('DATA_CRYPT_TYPE');
        $class = strpos($type, '\\') ? $type : 'Think\\Crypt\\Driver\\' . ucwords(strtolower($type));
        self::$handler = $class;
    }

    /**
     * 加密字符串
     * @param string $str 字符串
     * @param string $key 加密key
     * @param integer $expire 有效期（秒） 0 为永久有效
     * @return string
     */
    public static function encrypt($data, $key, $expire = 0) {
        if (empty(self::$handler)) {
            self::init();
        }
        $class = self::$handler;
        return $class::encrypt($data, $key, $expire);
    }

    /**
     * 解密字符串
     * @param string $str 字符串
     * @param string $key 加密key
     * @return string
     */
    public static function decrypt($data, $key) {
        if (empty(self::$handler)) {
            self::init();
        }
        $class = self::$handler;
        return $class::decrypt($data, $key);
    }

    /**
     * 生成唯一加密字符串
     * @param string $str 要加密的字符串
     * @param string $hash_per 前置key
     * @param string $_core_hash 后置key
     * @return string 加密后的字符串
     */
    public static function Generate_Hashcode($str, $hash_per = '', $_core_hash = '') {
        return md5($hash_per . $_SERVER['REMOTE_USER'] . $_SERVER['REMOTE_HOST'] . $_SERVER['HTTP_USER_AGENT'] . $str . $_core_hash);
    }

    /**
     * 加密串比较
     * @param type $src 原串
     * @param type $comp 要比较的串
     * @return boolean
     */
    public static function Crypt_Hash_Compare($src, $comp) {
        $temphash = explode('$', $src);
        if (sha1($temphash[0] . $comp) === $temphash[1]) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 加密
     * @param type $src
     * @param type $hashlen
     * @return type
     */
    public static function Crypt_Hash_Create($src, $hashlen = 6) {
        $return = self::Num_Rand($hashlen);
        return $return . '$' . sha1($return . $src);
    }

    private static function Num_Rand($lenth, $justnum = 0, $skipvars = array()) {
        mt_srand((double) microtime() * 1000000);
        $randval = '';
        for ($i = 0; $i < $lenth;) {
            $tvar = mt_rand(0, 9);
            if (in_array($tvar, $skipvars))
                continue;
            $randval.= $tvar;
            $i++;
        }
        if ($justnum == 0) {
            return substr(md5($randval), mt_rand(0, 32 - $lenth), $lenth);
        } else {
            return $randval;
        }
    }

}
